﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace gTray
{
    public partial class frmMain : Form
    {
        private frmNotify _NotificationForm;

        private NotifyIcon _TrayIcon;
        private ContextMenu _TrayMenu;

        private gTrayService _Controller;

        public frmMain()
        {
            InitializeComponent();

            CreateTrayMenu();

            _TrayIcon = new NotifyIcon();
            _TrayIcon.Text = "gTray";
            _TrayIcon.Icon = Properties.Resources.new_mail;

            _TrayIcon.Visible = true;

            _Controller = new gTrayService();
            _Controller.FeedUpdated += new FeedUpdatedHandler(_Controller_FeedUpdated);
        }

        private void CreateTrayMenu()
        {
            _TrayMenu = new ContextMenu();
            _TrayMenu.MenuItems.Add("View Inbox", OnViewInbox);
            _TrayMenu.MenuItems.Add("Check For New Emails", OnCheckNow);
            _TrayMenu.MenuItems.Add("-");
            _TrayMenu.MenuItems.Add("Edit Login Details", OnEditLogin);
            _TrayMenu.MenuItems.Add("Options", OnShowOptions);
            _TrayMenu.MenuItems.Add("-");
            _TrayMenu.MenuItems.Add("Exit", OnExit);
        }

        private void OnViewInbox(Object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://mail.google.com/mail/");
        }

        private void OnCheckNow(Object sender, EventArgs e)
        {
            _Controller.ForceUpdateFeed();
        }

        private void OnShowOptions(Object sender, EventArgs e)
        {
            _Controller.ShowOptionDialog(this);
        }

        private void OnEditLogin(Object sender, EventArgs e)
        {
            _Controller.ShowEditLoginDialog(this);
        }

        void _Controller_FeedUpdated(Feed UpdatedFeed)
        {
            this.BeginInvoke((MethodInvoker)delegate
            {
                if (UpdatedFeed.NewEntries.Count > 0)
                {
                    _NotificationForm = new frmNotify(UpdatedFeed);
                    _NotificationForm.Show(this);
                    _NotificationForm.NotificationFinished += new EventHandler(frmMain_NotificationFinished);

                    _TrayIcon.Text = "gTray" + Environment.NewLine + UpdatedFeed.Count + " unread emails!";
                }
            });
        }
        void frmMain_NotificationFinished(object sender, EventArgs e)
        {
            _NotificationForm.Dispose();
            _NotificationForm = null;
        }

        protected override void OnLoad(EventArgs e)
        {
            this.Visible = false;
            this.ShowInTaskbar = false;

            Init();

            base.OnLoad(e);
        }

        private void OnExit(object sender, EventArgs e)
        {
            _TrayIcon.Visible = false;
            Application.Exit();
        }

        private void Init()
        {
            bool doContinue = _Controller.Login(this);

            if (doContinue)
            {
                _TrayIcon.ContextMenu = _TrayMenu;
                _Controller.StartTimer();
            }
            else
                Application.Exit();
        }
    }
}
